﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using System.IO;
using System.Xml.Serialization;

namespace ZhangZhao.WebAPI
{
    public partial class UserAvatarComment : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string s = string.Empty;

            try
            {
                string UserName = HttpUtility.HtmlEncode(HttpContext.Current.Request[Parameter.UserName]);
                string Output = HttpUtility.HtmlEncode(HttpContext.Current.Request[Parameter.Output]);
                string Command = HttpUtility.HtmlEncode(HttpContext.Current.Request[Parameter.Command]);

                if (!string.IsNullOrWhiteSpace(UserName))
                {
                    ZhangZhao.BLL.H_UserAvatarComment bll = new ZhangZhao.BLL.H_UserAvatarComment();

                    string where = string.Empty;

                    if (!string.IsNullOrWhiteSpace(Command) && Command.Equals(CommandParameterValue.All, StringComparison.CurrentCultureIgnoreCase))
                    {
                        //获取全部头像的评论
                        where = string.Format(" UserAvatarId in ( select Id from H_UserAvatar with (nolock) where UserName = '{0}' )  ", UserName);
                    }
                    else
                    {
                        //获取当前头像的评论
                        where = string.Format(" UserAvatarId in ( select Id from H_UserAvatar with (nolock) where UserName = '{0}' and IsUse = 1 )  ", UserName);
                    }

                    List<ZhangZhao.Model.H_UserAvatarComment> list = bll.GetModelList(where);

                    if (!string.IsNullOrWhiteSpace(Output) && Output.Equals("json", StringComparison.CurrentCultureIgnoreCase))
                    {
                        s = JsonConvert.SerializeObject(list);
                    }
                    else
                    {
                        using (MemoryStream Stream = new MemoryStream())
                        {
                            XmlSerializer xml = new XmlSerializer(typeof(List<ZhangZhao.Model.H_UserAvatarComment>));

                            try
                            {
                                xml.Serialize(Stream, list);
                            }
                            catch { }

                            Stream.Position = 0;
                            using (StreamReader sr = new StreamReader(Stream))
                            {
                                s = sr.ReadToEnd();
                            }

                            xml = null;
                        }
                    }
                }
            }
            catch { }

            Response.Clear();
            Response.Write(s);
            Response.End();
        }
    }
}